// DONE:Initialze new and edit object from dialog
$(document).ready(function() {

        var formToSubmit = '#editObjectDialog_form';
        var formbutton = '#b-object';

        initNewObject = function()
        {  
            myObject[0]['ID'] = null;  
            myObject[0]['mode'] = 'NEW';  

        }

        initEditObject = function()
        {   
            myObject[0]['mode'] = 'EDIT';

            // select dropdowns
            $("#editObjectDialog_object_type").val(editObjectDialog_object_type);
            $("#editObjectDialog_object_type").attr("disabled","disabled");

            //disable type and set attributes visible
            $("#editObjectDialog_owner").val(editObjectDialog_owner);
            $("."+editObjectDialog_object_type).removeClass('hidden');

            //add input hidden object type for submission
            $('<input>').attr({
                    type: 'hidden',
                    id: 'editObjectDialog_object_type',
                    name: 'editObjectDialog_object_type',
                    value: editObjectDialog_object_type
            }).appendTo(formToSubmit);

            // for object_type change event
            myObject[0]['ID'] = editObjectDialog_post_ID;

            //get fields
            get_object_fields(editObjectDialog_object_type,myObject[0]['formEditUrl'],myObject[0]['ID'],formbutton)

        }

        // Must be in editObject.js due to adding multiple change event on load
        $('#editObjectDialog_object_type').live('change',function(){

                //DONE: rename $("#addNewObject").button("option", "disabled", false);
                myObject[0]['type'] = $(this).val();
                //$(formbutton).removeAttr("disabled");
                
                switch(myObject[0]['type']){
                    case 'apartment':
                        $('.apartment').removeClass('hidden');
                        $('.hotel').addClass('hidden');
                        $('.villa').addClass('hidden');
                        myObject[0]['type'] = 'apartment';
                        break;
                    case 'hotel':
                        $('.apartment').addClass('hidden');
                        $('.hotel').removeClass('hidden');
                        $('.villa').addClass('hidden');
                        myObject[0]['type'] = 'hotel';
                        break;
                    case 'villa':
                        $('.apartment').addClass('hidden');
                        $('.hotel').addClass('hidden');
                        $('.villa').removeClass('hidden');
                        myObject[0]['type'] = 'villa';
                        break;
                    default:
                        $('.apartment').addClass('hidden');
                        $('.hotel').addClass('hidden');
                        $('.villa').addClass('hidden');
                        $(formbutton).attr("disabled","disabled");
                        myObject[0]['type'] = null;
                        return false;
                }

                get_object_fields(
                    myObject[0]['type'],
                    myObject[0]['formViewUrl'],
                    myObject[0]['ID'],
                    formbutton
                );

                //log('change');

                return false;

        });          


        if(typeof(postAction)==='undefined')
            {

        }else{
            switch (postAction) {
                case "EDIT":
                    initEditObject();
                    $('.nav-tabs li').removeClass('hidden');
                    break;
                case "NEW":
                    initNewObject();
                    break;
            }   
        }

        //FORM VALIDATION
        jQuery(formToSubmit).validate({
                rules: {
                    /*newObjectDialog_object_type: { check_item_dropdown: true },
                    name: 'required' */
                }, 
                highlight: function(element) {
                    $(element).parent().parent().addClass("error");
                },
                unhighlight: function(element) {
                    $(element).parent().parent().removeClass("error");
                },
                submitHandler: function(form) {

                    //disable add button                            
                    $(formbutton).attr("disabled","disabled");

                    $(formToSubmit+' .status_info').html(loader_html());

                    var form = $(formToSubmit);
                    var form_data = form.serialize();

                    $.ajax({
                            url: form.attr('action'),
                            dataType: 'json',
                            type: "POST",
                            data: form_data,
                            success: function (data, textStatus, xhr) {

                                if(data.action==true){

                                    $(formToSubmit+' .status_info').html(alert_html(data.msg,'success')); 

                                    function action_delay(){                                                
                                        //window.location.href = base_url+'objects/view_all'; 
                                        alert(data.msg)
                                        $('.nav-tabs li').removeClass('hidden');
                                    }
                                    window.setTimeout(function() { action_delay(); }, 1001);

                                }else{

                                    $(formbutton).removeAttr("disabled");
                                    $(formToSubmit+' .status_info').html(alert_html(data.msg,'error'));
                                    $(formToSubmit).animate({
                                            scrollTop:  $(formToSubmit+' .status_info').offset().top
                                        }, 200);
                                }
                            },
                            error: function (xhr, textStatus, errorThrown) {
                                //enable add button                            
                                $(formbutton).removeAttr("disabled");
                                $(formToSubmit+' .status_info').html(alert_html('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n','error'));
                            }
                    });
                },
                errorPlacement: function(error, element){}
        })

});